/**
  ******************************************************************************
  * @file           : CAT1tf.h
  * @brief          : Header for CAT1tf.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __Cat1_H
#define __Cat1_H

#include "bsp.h"
#include "tskcfg.h"

#define CAT1_LINKA_Pin               GPIO_PIN_7
#define CAT1_LINKA_GPIO_Port         GPIOB
#define Cat1_LinkA()                 (GPIO_ISTAT(CAT1_LINKA_GPIO_Port) & CAT1_LINKA_Pin)

#define CAT1_LINKB_Pin               GPIO_PIN_6
#define CAT1_LINKB_GPIO_Port         GPIOB
#define Cat1_LinkB()                 (GPIO_ISTAT(CAT1_LINKB_GPIO_Port) & CAT1_LINKB_Pin)

#define CAT1_RST_Pin                 GPIO_PIN_9
#define CAT1_RST_GPIO_Port           GPIOB
#define Cat1_Reset(x)                x?(GPIO_BOP(CAT1_RST_GPIO_Port) = (uint32_t)CAT1_RST_Pin):(GPIO_BC(CAT1_RST_GPIO_Port) = (uint32_t)CAT1_RST_Pin)

#define CAT1_PWR_Pin                 GPIO_PIN_4
#define CAT1_PWR_GPIO_Port           GPIOE
#define Cat1_Power(x)                x?(GPIO_BOP(CAT1_PWR_GPIO_Port) = (uint32_t)CAT1_PWR_Pin):(GPIO_BC(CAT1_PWR_GPIO_Port) = (uint32_t)CAT1_PWR_Pin)

#define Cat1_UartEnable()            usart_enable(USART1)
#define Cat1_UartDisable()           usart_disable(USART1)

#define CAT1_CMDBUF_MAXLEN          127
#define CAT1_MAX_TMPBFLEN           128

#define CAT1_CMDQUE_LENGTH          5
#define CAT1_CMDQUE_ITEMSIZE        sizeof(Cat1_CmdBufType)


typedef struct
{
  char    buf[CAT1_CMDBUF_MAXLEN];
  uint8_t count;
}Cat1_CmdBufType;

typedef enum
{
  CAT1_CMD_OK       = 0x00U,
  CAT1_CMD_TIMEOUT,
	CAT1_CMD_ERRSTR,
	CAT1_CMD_EMPTYBF,
	CAT1_CMD_ERRCHECK,
	CAT1_CMD_ERRPARA,
  CAT1_CMD_ERRRES,
  CAT1_CMD_UNKNOWN1,
  CAT1_CMD_UNKNOWN2,
  CAT1_CMD_ERR4,
}Cat1_ErrTyp;

typedef enum
{
  SIMPLE_INS       = 0x00U,
	QUERY_STAT,
	CHECK_PARA,
	SET_PARA,
}Cat1_CmdTypeTyp;

typedef enum
{
	CAT1_MODE_TRS = 0x00U,
  CAT1_MODE_CMD,
	//CAT1_MODE_FTP,
}Cat1_ModeType;

typedef struct
{
	const char wkmod[8];
  const char cmdpw[12];
  const char stmsg[20];
  const char rstim[8];
  const char spden[8];
  const char naten[8];
  const char uaten[8];
	const char cachen[8];
	const char uartfl[8];
  const char uartft[8];
  const char socka[104];
	const char sockaen[8];
	const char sockben[8];
  const char sockasl[8];
	const char sockrstim[8];
	const char regen[8];
	const char hearten[8];
}Cat1_ConfigParaTyp;

typedef struct 
{
  const char     *cstr;   /* command string */
  const char     *rstr;   /* reply string */
  const int8_t   rty;     /* number of retries */
  const uint32_t timout;  /* receive wait timeout */
  const uint32_t rtytim;  /* retry interval time */
	const Cat1_CmdTypeTyp  cmdtype;

#ifdef SCRIPT_EXE
	const void     *errjmp;
#endif
}Cat1_CommandTyp;

typedef struct
{
  char ver[CAT1_MAX_TMPBFLEN];
  char sn[CAT1_MAX_TMPBFLEN];
  char imei[CAT1_MAX_TMPBFLEN];
  char iccid[CAT1_MAX_TMPBFLEN];
}Cat1_StaticInfoType;

typedef struct
{
  char csq[CAT1_MAX_TMPBFLEN];
  char syslink[CAT1_MAX_TMPBFLEN];
  char skalink[CAT1_MAX_TMPBFLEN];
}Cat1_DynamicInfoType;

/********************    +++ppp   ******************************/
extern const Cat1_CommandTyp cat1_cmd_ppp;
/********************    a   ******************************/
extern const Cat1_CommandTyp cat1_cmd_a;
/********************    Z   ******************************/
extern const Cat1_CommandTyp cat1_cmd_z;
/********************    S   ******************************/
extern const Cat1_CommandTyp cat1_cmd_s;
/********************    E   ******************************/
extern const Cat1_CommandTyp cat1_cmd_e;
/********************    ENTM   ******************************/
extern const Cat1_CommandTyp cat1_cmd_entm;
/********************    WKMOD   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_wkmod;
extern const Cat1_CommandTyp cat1_cmd_set_wkmod;
/********************    CMDPW   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_cmdpw;
extern const Cat1_CommandTyp cat1_cmd_set_cmdpw;
/********************    STMSG   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_stmsg;
extern const Cat1_CommandTyp cat1_cmd_set_stmsg;
/********************    RSTIM   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_rstim;
extern const Cat1_CommandTyp cat1_cmd_set_rstim;
/********************    SDPEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_spden;
extern const Cat1_CommandTyp cat1_cmd_set_spden;
/********************    NATEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_naten;
extern const Cat1_CommandTyp cat1_cmd_set_naten;
/********************    UATEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_uaten;
extern const Cat1_CommandTyp cat1_cmd_set_uaten;
/********************    CACHEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_cachen;
extern const Cat1_CommandTyp cat1_cmd_set_cachen;
/********************    CSQ   ******************************/
extern const Cat1_CommandTyp enet_cmd_csq;
/********************    SYSINFO   ******************************/
extern const Cat1_CommandTyp cat1_cmd_sysinfo;
/********************    VER   ******************************/
extern const Cat1_CommandTyp cat1_cmd_ver;
/********************    SN   ******************************/
extern const Cat1_CommandTyp enet_cmd_sn;
/********************    IMEI   ******************************/
extern const Cat1_CommandTyp enet_cmd_imei;
/********************    ICCID   ******************************/
extern const Cat1_CommandTyp enet_cmd_iccid;
/********************    CNUM   ******************************/
//extern const Cat1_CommandTyp enet_cmd_cnum;
/********************    UARTFL   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_uartfl;
extern const Cat1_CommandTyp cat1_cmd_set_uartfl;
/********************    UARTFT   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_uartft;
extern const Cat1_CommandTyp cat1_cmd_set_uartft;
/********************    SOCKA   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_socka;
extern const Cat1_CommandTyp cat1_cmd_set_socka;
/********************    SOCKAEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_sockaen;
extern const Cat1_CommandTyp cat1_cmd_set_sockaen;
/********************    SOCKBEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_sockben;
extern const Cat1_CommandTyp cat1_cmd_set_sockben;
/********************    SOCKALK   ******************************/
extern const Cat1_CommandTyp cat1_cmd_sockalk;
/********************    SOCKASL   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_sockasl;
extern const Cat1_CommandTyp cat1_cmd_set_sockasl;
/********************    SOCKRSTIM   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_sockrstim;
extern const Cat1_CommandTyp cat1_cmd_set_sockrstim;
/********************    REGEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_regen;
extern const Cat1_CommandTyp cat1_cmd_set_regen;
/********************    REGTP   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_regtp;
extern const Cat1_CommandTyp cat1_cmd_set_regtp;
/********************    REGDT   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_regdt;
extern const Cat1_CommandTyp cat1_cmd_set_regdt;
/********************    REGSND   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_regsnd;
extern const Cat1_CommandTyp cat1_cmd_set_regsnd; 
/********************    HEARTEN   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_hearten;
extern const Cat1_CommandTyp cat1_cmd_set_hearten;
/********************    HEARTTP   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_hearttp;
extern const Cat1_CommandTyp cat1_cmd_set_hearttp;
/********************    HEARTDT   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_heartdt;
extern const Cat1_CommandTyp cat1_cmd_set_heartdt;
/********************    HEARTTM   ******************************/
extern const Cat1_CommandTyp cat1_cmd_check_hearttm;
extern const Cat1_CommandTyp cat1_cmd_set_hearttm;

extern volatile Cat1_ModeType Cat1_WorkMode;

extern void Cat1_UartSendBuf(const uint8_t *buf, uint32_t len);
extern void Cat1_UartSendStr(const char *buf);

extern void Cat1_BspInit(void);
extern void Cat1_BspDeinit(void);
extern void Cat1_PowerOff(void);
extern uint8_t Cat1_ResetUp(void);
extern uint8_t Cat1_StaticConfig(void);
extern uint8_t Cat1_DynamicConfig(void);
extern uint8_t Cat1_WaitScoketALink(void);
#endif /* __Cat1_H */
